//
//  PhoneNumberLable.m
//  TabBar
//
//  Created by 陈德锋 on 16/4/14.
//  Copyright © 2016年 陈德锋. All rights reserved.
//

#import "PhoneNumberLable.h"

@implementation PhoneNumberLable

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

//可以响应的方法
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copyEx:))
    {
        return YES;
    }
    
    return NO;
}

//针对于响应方法的实现

-(void)copyEx:(UIMenuItem*)sender
{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    
    pboard.string = self.text;
    
    [self resignFirstResponder];
}

-(void)attachTapHandler
{
    self.userInteractionEnabled = YES;  //用户交互的总开关
    
    UILongPressGestureRecognizer *touch = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    
    touch.minimumPressDuration = 2;
    
    [self addGestureRecognizer:touch];
}

//绑定事件
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        [self attachTapHandler];
    }
    
    return self;
}

//同上
-(void)loadView
{
    [super awakeFromNib];
    
    [self attachTapHandler];
}

-(void)handleTap:(UIGestureRecognizer*)recognizer
{
    if (UIGestureRecognizerStateBegan == recognizer.state)
    {
        [self becomeFirstResponder];
        
        UIMenuItem *copyLink = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyEx:)];
        [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
        
        CGPoint location = [recognizer locationInView:[recognizer view]];
        CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
        [[UIMenuController sharedMenuController] setTargetRect:menuLocation inView:[recognizer view]];
        
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
    }
}


@end
